﻿<%
'''日志类
Class LogClass
	'日志输出等级: Error < Warning < Info < Debug
	'日志文件路径, Fso对象, 等级,格式,产生日志的页面
	Private s_path, s_fso, s_level, s_format, s_npage
	'Level的替代转化符,Path的替代转化符, Time的替代转化符
	Private s_lvp, s_pap, s_tmp
	'''构造
	Private Sub Class_Initialize()
		s_path = "/" & C_DataFolder & "/log/"
		s_level = "Infox"
		Set s_fso = CreateObject("Scripting.FileSystemObject")
		s_lvp = "{%level%}"
		s_pap = "{%path%}"
		s_tmp = "{%time%}"
		s_npage = Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERY_STRING")
		s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "] Path:[""" & s_pap & """]"
	End Sub

	'''析构
	Private Sub Class_Terminate()
		Set s_fso = Nothing
	End Sub
	
	'''格式化当前时间日期格式
	'p_t:格式话格式
	Private Function formatDt_(Byval p_t)
		Dim t_y : t_y = Year(Now())
		Dim t_m : t_m = IIF(Cint(Month(Now()))>=10, Month(Now()), "0"&Month(Now()))
		Dim t_d : t_d = IIF(Cint(Day(Now()))>=10, Day(Now()), "0"&Day(Now()))
		Select Case p_t
		Case 1
			Dim t_h : t_h = IIF(Cint(Hour(Now()))>=10, Hour(Now()), "0"&Hour(Now()))
			Dim t_n : t_n = IIF(Cint(Minute(Now()))>=10, Minute(Now()), "0"&Minute(Now()))
			Dim t_s : t_s = IIF(Cint(Second(Now()))>=10, Second(Now()), "0"&Second(Now()))
			formatDt_ = t_y & "-" & t_m & "-" & t_d & " " & t_h & ":" & t_n & ":" & t_s
		Case 2
			formatDt_ = t_y & t_m & "/" & t_d
		End Select
	End Function
	
	'''设置日志等级
	'p_s:Error < Warning < Info < Debug
	Public Property Let Level(Byval p_s)
		Select Case Lcase(p_s)
		Case "info"
			s_level = "Infox"
		Case "warning"
			s_level = "Warnx"
		Case Else
			s_level = p_s
		End Select
	End Property
	
	'''设置输出的路径。如果是文件夹路径则自动建立以时间为基准的日志文件,否则按指定路径输出
	'p_p:文件或文件夹路径
	Public Property Let Path(Byval p_p)
	     s_path = p_p
	End Property
	
	'''自定义格式化日志
	'p_s:格式化字符,特征值:1\2\3\simple\middle\full
	Public Property Let Format(Byval p_s)
		Select Case Lcase(Cstr(p_s))
		Case "1","simple"
			s_format = "Level:[" & s_lvp & "]"
		Case "2","middle"
			s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "]"
		Case "3","full"
			s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "] Path:[""" & s_pap & """]"
		Case Else
			s_format = p_s
		End Select
	End Property
	
	'''获取实际路径MapPath
	'p_p:路径
	Private Function absPath_(Byval p_p)
		If CStr(p_p) = "" Then
			absPath_ = "" : Exit Function
		End If
		If Mid(p_p,2,1)<>":" Then
			p_p = Server.MapPath(p_p)
		End If
		If Right(p_p,1) = "\" Then
			p_p = Left(p_p,Len(p_p)-1)
		End If
		absPath_ = p_p
	End Function

	'''判断服务器上的文件夹是否存在
	'p_p:文件夹路径
	Private Function isFolder_(Byval p_p)
		p_p = absPath_(p_p) : isFolder_ = False
		If s_fso.FolderExists(p_p) Then
			isFolder_ = True
		End If
	End Function
	
	'''在服务器上创建文件夹
	'p_p:路径
	Private Function createFolder_(Byval p_p)
		On Error Resume Next
		Dim t_p, t_a, t_i : createFolder_ = True
		t_p = absPath_(p_p)
		t_a = Split(t_p,"\") : t_p = ""
		For t_i = 0 To Ubound(t_a)
			t_p = t_p & t_a(t_i) & "\"
			If Instr(t_p, absPath_("/") & "\")>0 Then
				If Not isFolder_(t_p) And t_i>0 Then
					s_fso.CreateFolder(t_p)
				End If
			End If
		Next
		If Err.Number<>0 Then
			createFolder_ = False
			'收集错误,Log信息收集
			Errc.Throw(2)
		End If
		Err.Clear()
	End Function
	
	'''(更新/创建日志文件)
	'p_p:日志路径,如果是文件夹路径则自动建立以时间为基准的日志文件,否则按指定路径输出
	'p_s:单行日志内容
	Private Sub createAfile_(Byval p_p, Byval p_s)
		p_p  = Replace(p_p,"\","/")
		If InStrRev(p_p,".")<InStrRev(p_p,"/") Then
			p_p = p_p & "/" & formatDt_(2) & ".log"
		End If
		p_p  = Replace(p_p,"//","/")
		p_p = absPath_(p_p)
		Dim t_s : t_s = createFolder_(Left(p_p,InstrRev(p_p,"\")-1))
		If t_s Then
			Dim t_f
			If Not s_fso.FileExists(p_p) Then
				Set t_f = s_fso.CreateTextFile(p_p, True)
			Else
				Set t_f = s_fso.OpenTextFile(p_p, 8, True)
			End If
			t_f.WriteLine(p_s)
			t_f.Close
		Else
			Errc.Throw(1111)
		End If
	End Sub
	
	'写入日志
	'p_s:日志实体,不含前缀格式化
	Public Sub Write(Byval p_s)
		Dim t_t : t_t = s_format
	    t_t = Replace(t_t, s_lvp, s_level)
		t_t = Replace(t_t, s_pap, s_npage)
		t_t = Replace(t_t, s_tmp, formatDt_(1))
		createAfile_ s_path, t_t&" "&p_s
	End Sub
	
	'''写Debug日志
	'p_s:日志实体,不含前缀格式化
	Public Sub Debug(Byval p_s)
		s_level = "Debug"
		Write(p_s)
	End Sub
	
	'''写入Info日志
	'p_s:日志实体,不含前缀格式化
	Public Sub Info(Byval p_s)
		s_level = "Infox"
		Write(p_s)
	End Sub
	
	'''写入Warn日志
	'p_s:日志实体,不含前缀格式化
	Public Sub Warn(Byval p_s)
		s_level = "Warnx"
		Write(p_s)
	End Sub
	
	'''写入Error日志
	'p_s:日志实体,不含前缀格式化
	Public Sub [Error](Byval p_s)
		s_level = "Error"
		Write(p_s)
	End Sub
End Class
%>